package cn.tunyun.web.controller;

import cn.tunyun.UserServerApp;
import cn.tunyun.common.CommonResult;
import cn.tunyun.common.ResultCode;
import cn.tunyun.common.SendSms;
import cn.tunyun.domain.User;
import cn.tunyun.service.LoginService;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.net.www.protocol.https.DefaultHostnameVerifier;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController("/login")
public class LoginController {
    @Autowired
   private LoginService loginService;
//    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    public Object UserLogin(@RequestBody User user){
//        loginService.UserLogin(user);
//        return CommonResult.success();
//    }*/

    @RequestMapping(value="/login",method= RequestMethod.POST)
    public CommonResult login(@RequestBody String userName,String userPwd){
        try {
            User user = loginService.findByUsername(userName,userPwd );
            if (user == null || user.equals("")){
                return CommonResult.failed();
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.FAILED);

        }
    }
    //调短信接口
    @RequestMapping(value="/login")
    public CommonResult loginByPhone(@RequestBody String userName) {
        try {
            User user = loginService.findByUsername(userName);
            if (user == null || user.equals("")) {
                return CommonResult.failed("用户不存存在");
            }
            //用户名存在掉短信接口
            //SendSms.
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCode.FAILED);

        }
    }
    //用户注册接口
    @RequestMapping(value="/register")
    public CommonResult registerUser( String username, String password, String inviteCode, String InvitationCode, HttpServletRequest req) {
        String session = (String) req.getSession().getAttribute("code");
        if(InvitationCode.equals(session))
            try {
                loginService.register(username,password,inviteCode);
                //用户名存在掉短信接口
                return CommonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                return CommonResult.failed(ResultCode.FAILED);

            }
        return CommonResult.failed("验证码不一样");

    }
      /*  @RequestMapping(value="/login",method= RequestMethod.POST)
        public CommonResult login(@RequestBody String userName,String userPwd){
            try {
                User user = loginService.findByUsername(userName,userPwd );
                if (user == null || user.equals("")){
                    return CommonResult.failed();
                }
                return CommonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                return CommonResult.failed(ResultCode.FAILED);

            }
        }*/
}
